package com.teamwork.module03.controller;


import com.teamwork.common.entity.module03.requestentity.ReqArticleBody;

import com.teamwork.common.result.Result;
import com.teamwork.module03.service.ArticleService;
import com.teamwork.module03.service.AuthorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Slf4j
//@CrossOrigin
public class ArticleController {

    @Resource
    private ArticleService articleService;

    //移动端分页查询
    @GetMapping("/articles/select")
    public Result getArticles(Integer page,Integer size,String searchname){
       // System.out.println("接受到请求:"+page+size);
        return articleService.getArticles(page,size,searchname);
    }
    //管理端分页查询
    @PostMapping("/manger/articles")
    public Result getArticlesInManger(@RequestBody ReqArticleBody reqBody){

        return articleService.getArticlesInManger(reqBody);
    }

    //根据id查询文章
    @GetMapping("/articles/select/one")
    public Result getArticleById(Integer id){
        return  articleService.getArticleById(id);
    }
    //根据路径查询文字
    @GetMapping("/arts/{name}")
    public Result getTxt(@PathVariable("name")String name, HttpServletResponse response) throws FileNotFoundException {
       // System.out.println("执行了该方法、、、"+name);
        return articleService.getTxt(name,response);
    }

    //审核
    @PostMapping ("/manger/article")
    public Result checkArticle(@RequestBody ReqArticleBody queryBody){
       Integer id = queryBody.getId();
       Integer status = queryBody.getStatus();
//        System.out.println("id"+id+"status"+status);
      return  articleService.checkArticle(id,status);
    }

    //删除单个文章
    @DeleteMapping("/article/{id}")
    public Result deleteOne(@PathVariable("id") Integer id){
        return articleService.deleteOne(id);
    }
    //批量删除文章
    @PostMapping("/articles/deletebuch")
    public Result deleteBuch(@RequestBody Map<String, List<Integer>> map){
        List<Integer> ids = map.get("ids");
        return articleService.deleteBuch(ids);
    }

    //移动端查看文章的状态
    @GetMapping("/article/getcarestatus")
    public Result getCareStatus(Integer userid,Integer artid){
        log.info("userid:{},artid:{}",userid,artid);
        return articleService.getArticleCareStatus(userid,artid);
    }
    //移动端收藏文章
    @GetMapping("/article/care")
    public Result tocareArticle(Integer userid,Integer artid){
       // log.info("userid:{},artid:{}",userid,artid);
        return articleService.tocareArticle(userid,artid);
    }
    //移动端取消收藏
    @DeleteMapping("/article/delcare")
    public Result delcareArticle(Integer userid,Integer artid){
        return articleService.delcareArticle(userid,artid);
    }
    //移动端点赞文章
    @GetMapping("/article/like")
    public Result tolikeArticle(Integer userid,Integer artid){
        return articleService.tolikeArticle(userid,artid);
    }
    //移动端取消点赞文章
    @DeleteMapping("/article/dellike")
    public Result dellikeArticle(Integer userid,Integer artid){
        return articleService.delLikeArticle(userid,artid);
    }
    //移动端查询是否点赞文章
    @GetMapping("/article/getlikestatus")
    public Result getLikeArtStatus(Integer userid,Integer artid){
        return articleService.getLikeArtStatus(userid,artid);
    }

    //查询我发布的文章
    @GetMapping("/article/myart/{userid}")
    public Result getMyArticles(@PathVariable("userid")Integer userid){
        return articleService.getMyArticles(userid);
    }
    //查询我收藏的文章
    @GetMapping("/article/mystar/{userid}")
    public Result getMyStars(@PathVariable("userid")Integer userid){
        return articleService.getMyStars(userid);
    }


}
